package BVRNetworking;

import java.util.Vector;

import org.apache.mina.common.IoSession;

public class BVRN_Packet
{
	private Vector<String> parameters = new Vector<String>();
	private char packetType = BVRN_Definitions.BVRN_EMPTY;
	
	/**
	 * 
	 */
	public BVRN_Packet()
	{
		
	}
	
	/**
	 * 
	 * @param p
	 */
	public BVRN_Packet(String p)
	{
		if (p.length() <= 0)
			return;
		
		String separator = "" + BVRN_Definitions.BVRN_SEPARATOR;
		
		String[] arr = p.split(separator);
		
		this.packetType = p.charAt(0);
		
		for (int i = 1; i < arr.length; i++)
			this.parameters.add(arr[i]);
	}
	
	/**
	 * 
	 * @param packetType
	 */
	public BVRN_Packet(char packetType)
	{
		this.packetType = packetType;
	}
	
	/**
	 * 
	 * @return the packet type
	 */
	public char getType()
	{
		return this.packetType;
	}
	
	/**
	 * 
	 * @param packetType
	 */
	public void setType(char packetType)
	{
		this.packetType = packetType;
	}
	
	/**
	 * 
	 * @return the number of parameters in the packet
	 */
	public int getNumParameters()
	{
		return this.parameters.size();
	}
	
	/**
	 * 
	 * @param p
	 */
	public void addParameter(int p)
	{
		this.addParameter(Integer.toString(p));
	}
	
	/**
	 * 
	 * @param p
	 */
	public void addParameter(float p)
	{
		this.addParameter(Float.toString(p));
	}
	
	/**
	 * 
	 * @param p
	 */
	public void addParameter(char p)
	{
		String s = "" + p;
		
		this.addParameter(s);
	}
	
	/**
	 * 
	 * @param p
	 */
	public void addParameter(String p)
	{
		parameters.add(p);
	}
	
	/**
	 * 
	 * @param index
	 * @return the parameter at position index
	 */
	public String getParameter(int index)
	{
		if (index >= parameters.size())
			return "";
		
		return parameters.get(index);
	}
	
	/**
	 * 
	 * @param index
	 * @return the parameter at position index as a float value
	 */
	public float getParameterFloat(int index)
	{
		if (index >= parameters.size())
			return 0;
		
		return BVRN_StringUtils.StringToFloat(parameters.get(index));
	}
	
	/**
	 * 
	 * @param index
	 * @return the parameter at position index as an int value
	 */
	public int getParameterInt(int index)
	{
		if (index >= parameters.size())
			return 0;
		
		return Integer.parseInt(parameters.get(index));
	}
	
	/**
	 * 
	 * @return the parameters vector for the packet
	 */
	public Vector<String> getParameters()
	{
		return this.parameters;
	}
	
	/**
	 * 
	 * @param session
	 */
	public void send(IoSession session)
	{
		if (session == null)
		{
			System.out.println("Cannot send packet: null session.");
			
			return;
		}
		
		// no point sending empty packets
		if (this.packetType == BVRN_Definitions.BVRN_EMPTY)
			return;
		
		session.write(this.toString());
	}
	
	/**
	 * 
	 */
	public String toString()
	{
		String message = "";
		
		message += this.packetType;
		
		for (int i = 0; i < this.parameters.size(); i++)
		{
			message += BVRN_Definitions.BVRN_SEPARATOR;
			message += this.parameters.get(i);
		}
		
		return message;
	}
}
